<template>
	<view class="ChangePassword">
		<view class="box">
			<view class="input">
				<m-input class="m-input" type="password" displayable v-model="password" placeholder="请输入原密码（6-12位数字或字母）"></m-input>
			</view>
			<view class="input">
				<m-input class="m-input" type="password" displayable v-model="newpassword" placeholder="请输入新的密码（6-12位数字或字母）"></m-input>
			</view>
			<view class="input">
				<m-input class="m-input" type="password" displayable v-model="savepassword" placeholder="确认新的密码（6-12位数字或字母）"></m-input>
			</view>
		</view>

		<view class="btn">
			<button type="primary" @click="modify">立即修改</button>
		</view>
	</view>
</template>

<script>
	import mInput from '../../components/m-input.vue'
	export default {
		components: {
			mInput
		},
		data() {
			return {
				password: '',
				newpassword: '',
				savepassword: '',
				type: ''
			};
		},
		onLoad: function(option) {
			//获取路由传值
			this.type = option.type
			if (this.type == 'tran') {
				//动态修改title
				uni.setNavigationBarTitle({
					title: '修改交易密码'
				});
			}
			if (this.type == 'login') {
				//动态修改title
				uni.setNavigationBarTitle({
					title: '修改登录密码'
				});
			}
		},
		methods: {
			modify: function() {
				if (!this.password) {
					this.showtoast("请输入原密码")
					return false;
				}
				if (!this.newpassword) {
					this.showtoast("请输入新的密码")
					return false;
				}
				if (this.newpassword.length < 6 || this.newpassword.length > 12) {
					this.showtoast("新密码不合法")
					return;
				}
				if (this.savepassword !== this.newpassword) {
					this.showtoast("输入密码不一致")
					return false;
				}


				if (this.type == 'tran') {
					//修改交易密码

				}
				if (this.type == 'login') {
					//修改登录密码
					const data = {
						password: this.password,
						newpwd: this.newpassword,
						cmpwd: this.savepassword
					}
					this.$request.post("/api/user/changepwd", {
							data: data
						})
						.then(res => {
							this.showtoast(res.data.msg)
							if (res.data.code == 1) {
								uni.reLaunch({
									url: '../my/my',
								});
							}
						})
						.catch(error => {
							console.error('error:', error);
						});
				}
			},
			showtoast: function(text) {
				uni.showToast({
					title: text,
					icon: "none",
					duration: 2000
				});
			}
		}
	}
</script>

<style lang="less">
	.ChangePassword {
		width: 100%;
		display: flex;
		flex-direction: column;
		border-top: 5px solid #F1F1F1;

		.box {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;

			.input {
				width: 90%;
				height: 50px;
				border-bottom: 1px solid #ddd;
				display: flex;
				align-items: center;
				font-size: 14px;
			}
		}

		.btn {
			height: 80px;
			width: 80%;
			margin: 0 auto;
		}
	}
</style>
